package org.proteored.miapeapi.xml.msi.adapter;

import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.proteored.miapeapi.interfaces.Adapter;
import org.proteored.miapeapi.interfaces.msi.IdentifiedPeptide;
import org.proteored.miapeapi.interfaces.msi.IdentifiedProtein;
import org.proteored.miapeapi.interfaces.msi.IdentifiedProteinSet;
import org.proteored.miapeapi.interfaces.msi.InputDataSet;
import org.proteored.miapeapi.interfaces.msi.InputParameter;
import org.proteored.miapeapi.xml.msi.autogenerated.InputDataSetReferences;
import org.proteored.miapeapi.xml.msi.autogenerated.MSIIdentifiedProtein;
import org.proteored.miapeapi.xml.msi.autogenerated.MSIIdentifiedProteinSet;
import org.proteored.miapeapi.xml.msi.autogenerated.ObjectFactory;
import org.proteored.miapeapi.xml.msi.util.MSIControlVocabularyXmlFactory;
import org.proteored.miapeapi.xml.util.MiapeXmlUtil;

public class IdentifiedProteinSetAdapter implements
		Adapter<MSIIdentifiedProteinSet> {
	private final IdentifiedProteinSet proteinSet;
	private final ObjectFactory factory;
	private final MSIControlVocabularyXmlFactory cvFactory;
	private static Logger log = Logger.getLogger("log4j.logger.org.proteored");

	public IdentifiedProteinSetAdapter(IdentifiedProteinSet proteinSet,
			ObjectFactory factory, MSIControlVocabularyXmlFactory cvFactory) {
		this.cvFactory = cvFactory;
		this.factory = factory;
		this.proteinSet = proteinSet;

	}

	@Override
	public MSIIdentifiedProteinSet adapt() {
		MSIIdentifiedProteinSet proteinSetXML = factory
				.createMSIIdentifiedProteinSet();
		proteinSetXML.setName(proteinSet.getName());
		proteinSetXML.setFileURL(proteinSet.getFileLocation());

		Set<InputDataSet> inputDataSets = proteinSet.getInputDataSets();
		if (inputDataSets != null) {
			InputDataSetReferences inputDataSetRefs = factory
					.createInputDataSetReferences();
			for (InputDataSet inputDataSet : inputDataSets) {
				inputDataSetRefs.getInputDataSetRef().add(
						MiapeXmlUtil.IdentifierPrefixes.INPUTDATASET
								.getPrefix() + inputDataSet.getId());
			}
			proteinSetXML.setInputDataSetReferences(inputDataSetRefs);
		}
		InputParameter inputParameter = proteinSet.getInputParameter();
		if (inputParameter != null) {
			proteinSetXML
					.setParametersRef(MiapeXmlUtil.IdentifierPrefixes.PARAMETERS
							.getPrefix() + inputParameter.getId());
		}

		HashMap<String, IdentifiedProtein> identifiedProteins = proteinSet
				.getIdentifiedProteins();
		if (identifiedProteins != null) {
			int total = identifiedProteins.size();
			int i = 0;
			for (String proteinAcc : identifiedProteins.keySet()) {
				i++;
				IdentifiedProtein protein = identifiedProteins.get(proteinAcc);
				final List<IdentifiedPeptide> identifiedPeptides = protein
						.getIdentifiedPeptides();
				int numPeptides = 0;
				if (identifiedPeptides != null)
					numPeptides = identifiedPeptides.size();
				log.debug("Adapting protein ( " + i + "/" + total + ") "
						+ proteinAcc + " with " + numPeptides
						+ " peptides to XML");
				MSIIdentifiedProtein identifiedProteinXML = new IdentifiedProteinAdapter(
						protein, factory, cvFactory).adapt();
				proteinSetXML.getMSIIdentifiedProtein().add(
						identifiedProteinXML);

				/**
				 * Due to in the model the peptides are children of the protein,
				 * this loop add to the peptideSet variable all the peptides
				 * that are found in the protein
				 */
				// List<IdentifiedPeptide> identifiedPeptides =
				// protein.getIdentifiedPeptides();
				// if (identifiedPeptides != null) {
				// for (IdentifiedPeptide identifiedPeptide :
				// identifiedPeptides) {
				// peptideSetXML.getMSIIdentifiedPeptide().add(
				// new IdentifiedPeptideAdapter(identifiedPeptide, protein_id,
				// factory, cvFactory).adapt());
				// }
				// }
			}
		}
		return proteinSetXML;

	}

}
